FROM golang:alpine AS builder

LABEL stage=gobuilder

ENV CGO_ENABLED=0
ENV GOPROXY=https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

RUN apk update --no-cache && apk add --no-cache tzdata

WORKDIR /build

ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .

RUN go build -ldflags="-s -w" -o /app/main rpc/User/User.go
RUN mkdir -p /app/etc-template &&  \
    cp -r rpc/User/etc/. /app/etc-template/ &&  \
    cp /app/etc-template/Template.yaml /app/etc-template/conf.template.yaml


FROM alpine

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
ENV TZ=Asia/Shanghai

WORKDIR /app
COPY --from=builder /app/main /app/main
COPY --from=builder /app/etc-template/ /app/etc-template/

# 检查 /app/etc/conf.yaml 是否存在，如果不存在，则从/app/template/conf.template.yaml复制
ENTRYPOINT ["/bin/sh", "-c", \
    "[ -f /app/etc/conf.yaml ] && mkdir -p /app/etc/ || cp /app/etc-template/conf.template.yaml /app/etc/conf.yaml && exec /app/main -f /app/etc/conf.yaml"]

